﻿using INTEX.DataBase;
using INTEX.DataBase.Tools; 
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using WPFExcelReport.Chart;
using WPFExcelReport.Helpers;
using System.Threading;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class YGRVK02LIST : DocumentBase
    {

        #region=============Properties=======================
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string Mandant = "00";
        bool IsSetUp = false;
        YGRVK02LISTData vm;
    
        public DataSet setResult;
        ITXEXRPT ITXEXRPT;

        #region==========CollectionViewSource=================
        SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            }
        } 
        CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwMaster");
            }
        }
        DataRow dwMaster
        {
            get
            {
                if (vwMaster.View == null)
                    return null;
                if (vwMaster.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMaster.View.CurrentItem).Row;
            }
        }
        string ART
        {
            get
            {
                if (dwMaster == null)
                    return "";
                return dwMaster["VKPART"].ToString();

            }
        }
        public string KRS
        {
            get
            {
                return Helper.Right("  " + txtVKPAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtVKPAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return Helper.Right("000000" + txtVKPAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtVKPAUFLFD.Text = value;
            }
        }
        public string POS
        {
            get
            {
                return Helper.Right("000000" + txtVKPAUFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtVKPAUFPOS.Text = value;
            }
        }
        string Year
        {
            get
            {
                if (txtYear.Text.Trim() == "")
                {
                    MessageBox.Show("请设定年限");
                }
                return Helpers.Helper.Left(txtYear.Text.ToString() + "    ", 5).Trim();
            }
            set
            {
                txtYear.Text = value;
            }
        }
        string Month
        {
            get
            {
                if (txtMonth.Text.Trim() == "")
                {
                    MessageBox.Show("请设定月份");
                }
                return Helpers.Helper.Left(txtMonth.Text.ToString() + "    ", 5).Trim();
            }
            set
            {
                txtMonth.Text = value;
            }
        }
        string DelDate
        {
            get
            {
                if (txtDel.Text.Trim() == "")
                {
                    MessageBox.Show("请设定交期");
                }
                return Helpers.Helper.Left(txtDel.Text.ToString() + "99999999", 8).Trim();
            }
            set
            {
                txtDel.Text = value;
            }
        }
        string VRDue
        {
            get
            {
                if (txtDel.Text.Trim() == "")
                {
                    MessageBox.Show("请设定发货日期");
                }
                return Helpers.Helper.Left(txtVRDue.Text.ToString() + "99999999", 8).Trim();
            }
            set
            {
                txtVRDue.Text = value;
            }
        }
        string GBDue
        {
            get
            {
                if (txtGBDue.Text.Trim() == "")
                {
                    MessageBox.Show("请设定入库日期");
                }
                return Helpers.Helper.Left(txtGBDue.Text.ToString() + "99999999", 8).Trim();
            }
            set
            {
                txtGBDue.Text = value;
            }
        }

        #endregion
        BindingListCollectionView cvMaster
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMaster.ItemsSource);
            }
        }
        #endregion
        int total = 0;
        public YGRVK02LIST()
        {
            InitializeComponent();
            this.Name = "销售统计";
            vm = new YGRVK02LISTData();
            this.DataContext = vm;

            vm.Chart1 = this.Chart01;
            vm.Chart2 = this.Chart02;
            vm.Chart3 = this.Chart03;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                SetEnv.Clear();
                SetEnv.Merge(vm.LoadEnv());
                IsLoaded = true;
                vwMaster.View.CurrentChanged += vwMaster_CurrentChanged;
                vwEvents.View.CurrentChanged += vwEvents_CurrentChanged;
                KRS =Helper.Right(vm.year,2);                
                Year = vm.year;
                if (vm.month == "01")
                {
                    Year = (Convert.ToInt16(vm.year) - 1).ToString();
                    Month = "12";
                }
                else
                {
                    Year = vm.year;
                    Month = Helpers.Helper.Right("00" + (Convert.ToInt16(vm.month) - 1).ToString(), 2);
                }
                IsFinish.IsChecked = true;
                Month = "09";
                DelDate = vm.date;
                VRDue = vm.date;
                GBDue = vm.date;
                if (app.User.Trim() == "ITXV")
                {
                    vm.DEND = "20151231";
                    LFD = "000014"; 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        } 
        void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                string filter = "TXTIPIND ='" + dwMaster["VKPAUFKRS"].ToString() + dwMaster["VKPAUFLFD"].ToString() + "000IVDI                                             '";
                DataRow[] rows = SetMaster.ICC_TXTKON.Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "ICC_TXTKON";
                    SetDetail.ICC_TXTKON.Clear();
                    SetDetail.Merge(tb);
                    if (vwPTXT.View != null)
                    {
                        vwPTXT.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                }
              
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
                txtUserID.Text = app.User;
                txtPassword.Password = app.Pwd;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = Helper.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            expLog.IsExpanded = false;
        }
        #region========Query========

        private void btnQueryOTD_Click(object sender, RoutedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            DataSet SResult = new DataSet();
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {

                    SResult = vm.QueryOTD("00", KRS, LFD, ChkFinish, Year, Month);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                }
                
                if (vwMaster.View != null)
                {
                    vwMaster.View.MoveCurrentToFirst();
                }
                if (vwVKKOPF.View != null)
                {
                    vwVKKOPF.View.MoveCurrentToFirst();
                }
                var qz = from dwTemp in SetMaster.ICC_TXTKON.AsEnumerable()
                         where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                         select dwTemp;
                foreach (DataRow row in qz)
                {
                    txtPayment.Text = row["TXT010"].ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                dlgVK13.Visibility = Visibility.Collapsed;
                dlgMaster.Visibility = Visibility.Visible;
                dlgVR02.Visibility = Visibility.Collapsed;
                dlgVV05.Visibility = Visibility.Collapsed;
                dlgGBD.Visibility = Visibility.Collapsed;
            }

        }
        public delegate object AsyncRunHandle(object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null);

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = vm.BuildData("00", KRS, LFD);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult); 
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                    if (dwVKKOPF == null)
                        return;
                    var qz = from dwTemp in SetMaster.ICC_TXTKON.AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                             select dwTemp;
                    foreach (DataRow row in qz)
                    {
                        txtPayment.Text = row["TXT010"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                dlgVK13.Visibility = Visibility.Collapsed;
                dlgMaster.Visibility = Visibility.Visible;
                dlgVV05.Visibility = Visibility.Collapsed;
                dlgVR02.Visibility = Visibility.Collapsed;
                dlgGBD.Visibility = Visibility.Collapsed;
            }
        }
        private void btnQueryVK13_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = vm.QueryVKAN("00", KRS, LFD);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                }
                if (vwMaster.View != null)
                {
                    vwMaster.View.MoveCurrentToFirst();
                }
                if (vwVKKOPF.View != null)
                {
                    vwVKKOPF.View.MoveCurrentToFirst();
                }
                var qz = from dwTemp in SetMaster.ICC_TXTKON.AsEnumerable()
                         where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                         select dwTemp;
                foreach (DataRow row in qz)
                {
                    txtPayment.Text = row["TXT010"].ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                dlgVK13.Visibility = Visibility.Visible;
                dlgMaster.Visibility = Visibility.Collapsed;
                dlgVV05.Visibility = Visibility.Collapsed;
                dlgVR02.Visibility = Visibility.Collapsed;
                dlgGBD.Visibility = Visibility.Collapsed;

            }
        }
        private void btnQueryYGRVV05LIST_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
                //搜索
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = vm.BuildData("00", KRS, LFD, DelDate);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                    if (dwVKKOPF == null)
                        return;
                    var qz = from dwTemp in SetMaster.ICC_TXTKON.AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                             select dwTemp;
                    foreach (DataRow row in qz)
                    {
                        txtPayment.Text = row["TXT010"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                dlgVK13.Visibility = Visibility.Collapsed;
                dlgMaster.Visibility = Visibility.Collapsed;
                dlgVV05.Visibility = Visibility.Visible;
                dlgVR02.Visibility = Visibility.Collapsed;
                dlgGBD.Visibility = Visibility.Collapsed;
            }
        }
        private void btnQueryVR02_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
                //搜索
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = vm.QueryVR02("00", KRS, LFD, VRDue);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                    if (dwVKKOPF == null)
                        return;
                    var qz = from dwTemp in SetMaster.ICC_TXTKON.AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                             select dwTemp;
                    foreach (DataRow row in qz)
                    {
                        txtPayment.Text = row["TXT010"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                dlgVK13.Visibility = Visibility.Collapsed;
                dlgMaster.Visibility = Visibility.Collapsed;
                dlgVV05.Visibility = Visibility.Collapsed;
                dlgVR02.Visibility = Visibility.Visible;
                dlgGBD.Visibility = Visibility.Collapsed;
            }
        }
        private void btnQueryGBDaily_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
                //搜索
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = vm.QueryGBDaily("00", GBDue);
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                    if (dwVKKOPF == null)
                        return;
                    var qz = from dwTemp in SetMaster.ICC_TXTKON.AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                             select dwTemp;
                    foreach (DataRow row in qz)
                    {
                        txtPayment.Text = row["TXT010"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                dlgVK13.Visibility = Visibility.Collapsed;
                dlgMaster.Visibility = Visibility.Collapsed;
                dlgVV05.Visibility = Visibility.Collapsed;
                dlgGBD.Visibility = Visibility.Visible;
                dlgVR02.Visibility = Visibility.Collapsed;
            }
        }
        private void txtVKPAUFLFD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
            {
                return;
            }
            btnQuery_Click(null, null);
        }

        #endregion
        #region========Print========
        private void btnPrintYGRVK13LIST_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetMaster;
            ITXEXRPT.dsSource.Merge(SetEnv);

            ITXEXRPT.DocType = app.company + "VK13LIST";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintYGRPPOLIST_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetMaster;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "PPOLIST";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintYGRVV05LIST_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetMaster;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "VV05LIST";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }
        private void btnPrintYGRVR02LIST_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetMaster;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "VR02LIST";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintYGRGBDAILY_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetMaster;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "GBDAILY";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintYGRVKOTD_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetMaster;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "VKOTD";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }
        private void btnPrintYGRVR02KD_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetMaster;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "VR02KD";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        #endregion 

         
    }
}
